//
//  ZGrantCheckDetailVC.m
//  DistributorAB
//
//  Created by LZY on 2019/10/15.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ZGrantCheckDetailVC.h"

#import "ZGrantStateCell.h"
#import "BApplyForMoneyDetailCell.h"
#import "BApplyForMoneyReasonCell.h"

#import "ZGrantDetailModel.h"
#import "BApplyForMoneyVC.h"//申请补助金

@interface ZGrantCheckDetailVC () <UITableViewDelegate,UITableViewDataSource,ZGrantStateCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZGrantDetailModel *grantDetailModel;
@end

@implementation ZGrantCheckDetailVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    // 禁用返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    // 开启返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"查看详情";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = kColorWithHex(0xf2f2f2);
    
    [self setupViewAction];
    
    [self requestData];
}
#pragma mark - 返回按钮点击事件
- (void)leftClick{
    if (_isFromDeal) {
        //返回指定的界面
        if (self.navigationController.viewControllers.count > 2) {
            UIViewController *vc = [self.navigationController.viewControllers objectAtIndexSafe:1];
            [self.navigationController popToViewController:vc animated:YES];
        }
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark - 界面布局
- (void)setupViewAction{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, kScreen_Width, kScreen_Height - kNaviHeight) style:UITableViewStylePlain];
    _tableView.backgroundColor = kColorWithHex(0xf2f2f2);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[ZGrantStateCell class] forCellReuseIdentifier:NSStringFromClass([ZGrantStateCell class])];
    [_tableView registerClass:[BApplyForMoneyDetailCell class] forCellReuseIdentifier:NSStringFromClass([BApplyForMoneyDetailCell class])];
    [_tableView registerClass:[BApplyForMoneyReasonCell class] forCellReuseIdentifier:NSStringFromClass([BApplyForMoneyReasonCell class])];
    
}
#pragma mark - 数据请求
- (void)requestData{
    kWeakSelf;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObjectIfNotNil:self.grantID forKey:@"id"];
    [NetWorkTool POST:USER_GRANTS_INFO param:param success:^(id dic) {
        weakSelf.grantDetailModel = [ZGrantDetailModel mj_objectWithKeyValues:[dic objectForKey:@"data"]];
        [weakSelf.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.grantDetailModel) {
        return 3;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //市县级
    if (indexPath.row == 0) {
        ZGrantStateCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZGrantStateCell class]) forIndexPath:indexPath];
        cell.grantDetailModel = self.grantDetailModel;
        cell.delegate = self;
        return cell;
    }else if (indexPath.row == 1){
        BApplyForMoneyDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BApplyForMoneyDetailCell class]) forIndexPath:indexPath];
        cell.grantDetailModel = self.grantDetailModel;
        return cell;
    }else if (indexPath.row == 2){
        BApplyForMoneyReasonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BApplyForMoneyReasonCell class]) forIndexPath:indexPath];
        cell.isGrantDetail = YES;
        cell.grantDetailModel = self.grantDetailModel;
        return cell;
    }
    return [UITableViewCell new];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        NSInteger stateInteger = [self.grantDetailModel.state integerValue];
        if (stateInteger == 2 || stateInteger == 4) {
            return [ZGrantStateCell cellHeightRefuse];
        }else{
            return [ZGrantStateCell cellHeightNormal];
        }
    }else if (indexPath.row == 1){
        return [BApplyForMoneyDetailCell cellHeight];
    }else if (indexPath.row == 2){
        return [BApplyForMoneyReasonCell cellHeight];
    }
    return 0;
}
#pragma mark - ZGrantStateCellDelegate
- (void)grantStateCellApplyAgain{
    BApplyForMoneyVC *vc = [BApplyForMoneyVC new];
    vc.grantDetailModel = self.grantDetailModel;
    vc.isFromGrantDetail = YES;
    vc.grantID = self.grantDetailModel.Id;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
